<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class OperationController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
	}

	function index(){
 		$userName = I('param.userName');
 		$sex = I('param.sex');
 		$onickName = I('param.onickName');
 		if($userName){
 			$where = "&ouserName=$userName";
				
 		}
 		if($onickName){
 		 			$where .= "&onickName=$onickName";
 		
 		}
 		if($sex !== '全部' && is_numeric($sex)){
 			$where .= "&sex=$sex";

 		}
 		
 		$trtotal = $this->curl_http(C('user_admin_url').'operator/queryOperatorList?token='.$_SESSION["TOKEN"].'&curPage=1&pageSize=1'.$where);
 		$page = $this->page($trtotal['ReturnData']['page']['totalCount'],20);
 		$result = $this->curl_http(C('user_admin_url').'operator/queryOperatorList?token='.$_SESSION["TOKEN"]."&curPage=".$page->Current_page."&pageSize=".$page->Page_size.$where);
 		/* if($result['ReturnData']['list']){
 		    foreach($result['ReturnData']['list'] as $key=>$val){
 		        $live = $this->curl_http(C('video_url').'live/getCountByLiveOpId?token='.$_SESSION["TOKEN"].'&operatorId='.$val["operatorId"]);
 		        $result['ReturnData']['list'][$key]['liveCount'] = $live['ReturnData']['count'];
 		        $commentary = $this->curl_http(C('video_url').'live/getCountByCommentaryOpId?token='.$_SESSION["TOKEN"].'&operatorId='.$val["operatorId"]);
 		        $result['ReturnData']['list'][$key]['commentaryCount'] = $commentary['ReturnData']['count'];
 		    }
 		     
 		     
 		} */
 		$this->assign("data", $result['ReturnData']['list']);
 		$this->assign("page", $page->show('Admin'));
		$this->display();
	}
	function detail(){
	    $operatorId = I('param.operatorId')?I('param.operatorId'):I('param.id');
	    if($operatorId){
	        $where = "&operatorId=$operatorId";
 		}
	    $result = $this->curl_http(C('user_admin_url').'operator/queryOperatorByOperatorId?token='.$_SESSION["TOKEN"].$where);
	    $this->assign("operator", $result['ReturnData']['operator']);
	    $this->display();
	}
    public function open($id)
    {
        if ($id > 0) {
            $result = $this->curl_http(C('user_admin_url').'operator/auditOperator?isAudit=1&operatorId='.$id);
            if ($result['ResultCode'] == '0000') {
                $arr = array("status" => 1, "info" => '启用成功');
            } else {
                $arr = array("status" => 0, "info" => '非常抱歉，系统繁忙');
            }
        } else {
            $arr = array("status" => 0, "info" => '参数错误');
        }
        $this->ajaxReturn($arr);
    }

    public function close($id)
    {
        if ($id > 0) {
            $result = $this->curl_http(C('user_admin_url').'operator/auditOperator?isAudit=0&operatorId='.$id);
             if ($result['ResultCode'] == '0000') {
                $arr = array("status" => 1, "info" => '禁用成功');
            } else {
                $arr = array("status" => 0, "info" => '非常抱歉，系统繁忙');
            }
        } else {
            $arr = array("status" => 0, "info" => '参数错误');
        }
        $this->ajaxReturn($arr);
    }
    function LiveTask(){
        $id = I('param.id');
        $result = $this->curl_http(C('video_url').'live/getListByLiveOpId?token='.$_SESSION["TOKEN"].'&operatorId='.$id);
        $this->assign("data", $result['ReturnData']);
        $this->assign('id',$id);
        $this->display('livetask');
    }
    function CommentaryTask(){
        $id = I('param.id');
        $result = $this->curl_http(C('video_url').'live/getListByCommentaryOpId?token='.$_SESSION["TOKEN"].'&operatorId='.$id);
        $this->assign("data", $result['ReturnData']);
        $this->assign('id',$id);
        $this->display('commentarytask');
    }
    
    function add(){
        $this->display();
    }
    function add_post(){
        $params = array(
            'ouserName' => I("param.ouserName"),
            'password' => I("param.password"),
            'onickName' => I("param.onickName"),
            /* 'operatorType' => I("param.operatorType"), */
            'sex' => I("param.sex"),
            'depart' => I("param.depart"),
            'position' => I("param.position"),
            'userUrl' => I("param.userUrl"),
         
        );
        if(!$params['ouserName']){
            $this->error("请填写账号！");
        }
        if(!$params['onickName']){
            $this->error("请填写昵称！");
        }
       /*  if (!$params['userUrl']) {
            $this->error("请上传头像！");
        } else { '&operatorType=' . $params['operatorType'] .
            $params['userUrl'] = "http://{$_SERVER['HTTP_HOST']}" . $params['userUrl'];
        } */
        if ($params['userUrl']) {
            $where = '&userUrl=' . $params['userUrl'];
        }
        $fields = '&ouserName=' . $params['ouserName'] . '&password=' . $params['password'] . '&onickName=' . $params['onickName'] .  '&sex=' . $params['sex']. '&depart=' . $params['depart'] . '&position=' . $params['position']. $where;
        $result = $this->curl_http(C('user_admin_url').'operator/register?token='.$_SESSION["TOKEN"],$fields);
        if($result['ResultCode'] == '0000'){
            $this->success("添加成功！", U("Admin/Operation/index"));
        }else{
            return $this->error($result['Message']);
        }
    
    }
    function edit(){
        $params = array(
            'operatorId' => I("param.operatorId"),
            'ouserName' => I("param.ouserName"),
            'onickName' => I("param.onickName"),
            'operatorType' => I("param.operatorType"),
            'sex' => I("param.sex"),
            'depart' => I("param.depart"),
            'position' => I("param.position"),
            'userUrl' => I("param.userUrl"),
         
        );
        $this->assign('data',$params);
        $this->display();
    }
    function edit_post(){
         $params = array(
            'operatorId' => I("param.operatorId"),
            'onickName' => I("param.onickName"),
            'operatorType' => I("param.operatorType"),
            'sex' => I("param.sex"),
            'depart' => I("param.depart"),
            'position' => I("param.position"),
            'userUrl' => I("param.userUrl"),
         
        );
       /*  if (!$params['userUrl']) {
            $this->error("请上传头像！");
        } else {
            if((strpos($params['userUrl'], 'http://') !== false) or (strpos($params['userUrl'], 'https://') !== false)){
                $params['userUrl'] =  $params['userUrl'];
            }else{
                $params['userUrl'] = "http://{$_SERVER['HTTP_HOST']}" . $params['userUrl'];
            }
        } */
        $fields = '&operatorId=' . $params['operatorId'] .'&onickName=' . $params['onickName'] . '&operatorType=' . $params['operatorType'] . '&sex=' . $params['sex']. '&depart=' . $params['depart'] . '&position=' . $params['position']. '&userUrl=' . $params['userUrl'];
        $result = $this->curl_http(C('user_admin_url').'operator/updateOperator?token='.$_SESSION["TOKEN"],$fields);
    /*     echo C('user_admin_url').'operator/updateOperator?token='.$_SESSION["TOKEN"].$fields.'<br/>';
        print_r($result); */
        if($result['Success']){
            $this->success("修改成功！", U("Admin/Operation/index"));
        }else{
            return $this->error($result['Message']);
        }
    }
    
    function del(){
        $operatorId = I("param.operatorId",0);
        $result = $this->curl_http(C('user_admin_url').'operator/deleteOperator?token='.$_SESSION["TOKEN"].'&operatorId='.$operatorId);
        if($result['ResultCode'] == '0000'){
            $this->success("删除成功！", U("Admin/Operation/index"));
        }else{
            return $this->error($result['Message']);
        } 
    }
}